<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;

/**
* 职位管理
*/
class Job extends AdminBase
{
	
	protected function _initialize()
	{
		parent::_initialize();
	}
	public function index(){
        $data = input('param.');
        if ($data['title']) {
            $where['jobname|jobid'] = ['like',"%{$data['title']}%"];
            $list = model('job')->where($where)->field('jobid,cid,sort,reading,is_show,jobname,add_time,update_time')->order('jobid desc')->paginate(15);
        }else{
            $list = model('job')->field('jobid,cid,sort,reading,is_show,jobname,add_time,update_time')->order('jobid desc')->paginate(15);
        }   
		return $this->fetch('index',['list'=>$list]);
	}
	public function add(){
        $company_list = db('company')->field('id,name')->order('sort desc,add_time desc')->select();
		return $this->fetch('add',['company_list'=>$company_list]);
	}
	public function save(){

		if($this->request->isPost()){

            $data = $this->request->except('wage,job_specification,job_requirements', 'post');

            if ($_POST['wage']) {
                $data['wage'] = $_POST['wage'];
            }else{
                $data['wage'] = '面议';
            }

          /*  if(in_array($_POST['province'],array('北京','天津','上海','重庆'))){
                $data['city']=$_POST['province'];

            }*/

            $data['job_specification'] = $_POST['job_specification'];
            $data['job_requirements'] = $_POST['job_requirements'];
			if(db('job')->insert($data) !== false){
				$this->success('添加成功','index');
			}else{
				$this->error('提交失败');
			}
		}
	}
	public function edit($id){
   /*     $rows=db('job')->find($id);
        if($rows['province']==''){
            $rows['province']=$rows['city'];
            $rows['city']='';
        }*/
		return $this->fetch('edit',['rows'=>db('job')->find($id),'company_list'=>db('company')->field('id,name')->select()]);
	}
	public function update(){
 		if($this->request->isPost()) {
            $data = $this->request->except('wage,job_specification,job_requirements', 'post');
            if ($_POST['wage']) {
                $data['wage'] = $_POST['wage'];
            }else{
                $data['wage'] = '面议';
            }
            $data['job_specification'] = $_POST['job_specification'];
            $data['job_requirements'] = $_POST['job_requirements'];
            if(db('job')->update($data) !== false){
                $this->success('更新成功','index');
            }else{
                $this->error('提交失败');
            }
	   }
    }
	/*
	 * 更新'0'或'1'
	 */
	public function flag($id){
        if (request()->isPost()) {
            $data = input('post.');
            if (db('job')->where(['jobid' => $id])->update($data) !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }
    }
    public function delete($id = 0, $ids = [])
    {
        
        $id = $ids ? $ids : $id;
        if ($id) {
            if (db('job')->delete($id)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('请选择需要删除的项目');
        }
    }
}